<?php

//require_once 'VO/VOCountry.php';
require_once 'VO/VOPlace.php';
//require_once 'VO/Trip_place.php';
require_once 'VO/VOTrip.php';
require_once 'VO/VOUser.php';
require_once 'VO/VOQuestion.php';
require("config.inc.php"); 

		
$test = new DatabaseWrapper();
$user = new VOUser();
$user->address = "address";
$user->id = "id";
$user->age = "age";
$user->lastname = "lastname";
$user->name = "name";
$user->password = "password";
$user->photo = "photo";
$user->username = "username";
//$test->register($user);
//$test->login("test_user","test_user");
//$test->getQuestions();
//$result= $test->saveTrip();
//$result= $test->addPlacetoTrip("","");

//$result=$test->saveTripInformation("");
		
class DatabaseWrapper 
{
	
//	public function getQuestions() 
//	{
//		$DATABASE_SERVER = "91.121.56.68";
//		$DATABASE_PASSWORD = "sixfeetunder";
//		$DATABASE_USERNAME = "root";
//		$DATABASE_NAME = "adaugusta2";
//		
//		//connect to the database.
//		//we could have used an abstracting layer for connecting to the database.
//		//for the sake of simplicity, I choose not to.
//		$mysql = mysql_connect($DATABASE_SERVER, $DATABASE_USERNAME, $DATABASE_PASSWORD);
//		mysql_select_db($DATABASE_NAME);
//		//retrieve all rows
//		$query = "SELECT ada_q_en.question,
//				       ada_q_en.id_q,
//				       ada_q_en.answer_1,
//				       ada_q_en.answer_2,
//				       ada_q_en.answer_3,
//				       ada_q_en.answer_4,
//				       ada_q_en.correct
//			  	   FROM adaugusta2.ada_q_en ada_q_en
//				   WHERE (ada_q_en.is_translated = 1) OR (ada_q_en.is_active = 0)
//				   ORDER BY rand()
//				   LIMIT 3";
//		$result = mysql_query($query);
//
//		$ret = array();
//		
//	    while ($row = mysql_fetch_object($result, "VOQuestion")) 
//	    {
//			$ret[] = $row;
//		}
//		
//		// free the memory used by the mysql handler
//		mysql_free_result($result);
//		// return the result
//		return $ret;
//	}
	
	
	/**********************************************
	 *   Constructor
	 *   	
	 *   Connecting to the databse 
	 */
 	public function __construct()
 	{
 	 	//connect to the database.
		//we could have used an abstracting layer for connecting to the database.
		//for the sake of simplicity, I choose not to.
		mysql_connect(DB_SERVER, DB_USER, DB_PASS);
		mysql_select_db(DB_DATABASE);
 	 	
 	}	
	
	
	public function fetchAll_Trip($user_id) 
	{
		
		//retrieve all rows
		$query = "SELECT * FROM trip as t where t.user_id=".$user_id;
		$result = mysql_query($query);

		$ret = array();
		
	    while ($row = mysql_fetch_object($result, "VOTrip")) 
	    {
			$ret[] = $row;
		}
		
		foreach($ret as $current_trip)
		{
			 $new_trip_id=$current_trip->trip_id;
			  	/***********get places list for a trip by "trip_id""**************/
      		  $query = "SELECT p.name,p.lat,p.lon 
      		  			FROM trip_place as tp 
      		  			INNER JOIN place p ON p.id = tp.place_id where tp.trip_id=21";
        	  $place_list = mysql_query($query);

        	  // places list
        	  $places = array();
			  while ($row = mysql_fetch_object($place_list, "VOPlace")) 
			  {
					$places[] = $row;
			  }
			  $current_trip-> places_list = $places;     
		}
		
		// free the memory used by the mysql handler
		mysql_free_result($result);
		// return the result
		return $ret;
	}
	
	/**
	 * Login service with the pasic parameters
	 * @param unknown_type $username
	 * @param unknown_type $password
	 */
	public function login($username, $password)
	{
		$query = "SELECT * FROM user as currentUser 
				  where currentUser.username='".$username ."' and currentUser.password='".$password."'";
		
		$result = mysql_query($query);
		$user = mysql_fetch_object($result, "VOUser");
		
		// return the user
		return $user;
	}
	
	public function register ($user)
	{
		$query = "INSERT INTO user 
				  VALUES ('".$user->id."','"
						   .$user->name."','"
						   .$user->lastname."','"
						   .$user->username."','"
						   .$user->password."','"
						   .$user->mail."','"
						   .$user->address."',"
						   .$user->age.",'"
						   .$user->photo."')"  ;
						   
		$result = mysql_query($query);	

		return $result;
		
	}
	

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	/****************************************
	 *  Attributes
	 ***************************************/
	// the database object wich holds the connection

//	
//	/**
//	 *  Based on the user Id gets all the trips
//	 *  @param $user_id: the id of the user whose trips will be feched
//	 */ 
//	public function fetchAll_Trip($user_id)
//	{
//		
//		// connecting to the database
//		
//		
//		$Trip1 = new Trip($this->db);
//		$user_trips = $Trip1->fecthAll($user_id);
//		
//		/*************OUTPUT: ARRAY FORMAT********************/
//		/*$Trip1 = new Trip($this->db);
//		$Trip1->name = 'Trip3';			
//		/*************place_id=15********************/
//		//PLACE
//		/*$place_o1 = new Place($this->db);
//		$place_o1->name = "Rome";
//		$place_o1->latitude = 5.5;
//		$place_o1->long = 4.4;
//		//TRIPPLACE
//		$place_o1->description = 4.4;
//		$place_o1->estado = 4.4;		
//		$places_list= array($place_o1,$place_o2);*/
//	
//		return($user_trips);		
//		
//	}
	
	

//	public function getCountries()
//	{	
//		$ret;
//		/*$DATABASE_SERVER = "91.121.56.68";
//		$DATABASE_PASSWORD = "vub_wis";
//		$DATABASE_USERNAME = "wis_developer";
//		$DATABASE_NAME = "tours";
//		
//		$mysql = mysql_connect($DATABASE_SERVER, $DATABASE_USERNAME, $DATABASE_PASSWORD);
//		mysql_select_db($DATABASE_NAME);
//		//retrieve all rows
//		$query = "SELECT * FROM country";
//		$result = mysql_query($query);
//
//		$ret = array();
//		
//	    while ($row = mysql_fetch_object($result, 'VOCountry')) 
//	    {
//			$ret[] = $row;
//		}
//		
//		// free the memory used by the mysql handler
//		mysql_free_result($result);
//		// return the result*/
//		return $ret;
//	}
	
	
//	public  function init_data()
//	
//	{
//		
//		$place_o1 = new Place($this->db);
//		$place_o1->name = "Madrid";
//		$place_o1->latitude = 1.1;
//		$place_o1->long = 2.1;
//	
//		$place_o2 = new Place($this->db);
//		$place_o2->name = "Guayquil";
//		$place_o2->latitude = 3.1;
//		$place_o2->long = 4.1;
//		
//		$places_list= array($place_o1,$place_o2);
//		
//		
//		$Trip1 = new Trip($this->db);
//		$Trip1->name = 'Trip-test';
//		$Trip1->user_id = 2;
//		$Trip1->places_list = $places_list;
//		
//		/*$Trip2 = new Trip($this->db);
//		$Trip2->name = 'Trip5';		
//		$Trip2->user_id = 2;
//		$Trip2->places_list = $places_list;*/
//		
//		$list_trips = array($Trip1);
//		return($list_trips);
//		
//	}
	
//	//(insert or update a trip) input: $Trip class  
//	public function saveTrip()
//	{
//		
//		$list_trips = $this->init_data();
//		$result = 1;
//		foreach($list_trips as $current_trip)
//		{
//			//$trip->id (exist), make an UPDATE
//			//else
//			
//			 $new_trip_id=$current_trip->insert();
//			 foreach($current_trip->places_list as $place)
//			 {
//				  // if that place dont exist in DB, insert!!!
//					$new_place_id =$place->insert();
//					
//					// if INSERT was ok, fill TRIP_PLACE table!
//					$new_trip_place = new Trip_place($this->db);
//					$new_trip_place->place_id=$new_place_id;
//					$new_trip_place->trip_id=$new_trip_id;
//					$new_trip_place_id = $new_trip_place->insert(); 
//						  				
//			 }	
//		}
//		
//		
//		
//		return($result);
//		
//	}
	
//	
	//save a single trip
//	public function saveTripInformation($trip_object)
//	{
//		//$trip_objectnew Place($this->db);
//		$trip_object->name= 'trip name';
//		$trip_object->description =  'trip descriptio';
//		$trip_object->user_id =  2;
//		/******************saving just in TRIP***************/
//		$Trip1 = new Trip($this->db);
//		$Trip1->name = $trip_object->name;
//		$Trip1->description = $trip_object->description;
//		$Trip1->user_id = $trip_object->user_id;
//	
//	}
//	//update information TRIP(db trip)
//	/*public function updateTripInformation($trip_object)
//	{
//		//$trip_objectnew Place($this->db);
//		$trip_object->name= 'trip name';
//		$trip_object->description =  'trip descriptio';
//		$trip_object->user_id =  2;
//		/******************saving just in TRIP***************/
//		/*$Trip1 = new Trip($this->db);
//		$Trip1->name = $trip_object->name;
//		$Trip1->description = $trip_object->description;
//		$Trip1->user_id = $trip_object->user_id;
//		$Trip1->update();*/	
//	//}
//	public function addPlacetoTrip($trip_id,$place)
//	{
//		
//		$place_o1 = new Place($this->db);
//		$place_o1->name = "test-place";
//		$place_o1->latitude = 5.5;
//		$place_o1->long = 4.4;
//		
//		$trip_id=21;
//		$sql_trip = "SELECT * FROM trip as t where t.trip_id=".$trip_id;       
//        $trip = $this->db->fetch_array($sql_trip);
//        //echo 'trip'.$trip[];
//        //insert first in place in PLACE 
//        //$new_place_id =$place_o1->insert();					
//		// if INSERT was ok, fill TRIP_PLACE table!
//		//$new_trip_place = new Trip_place($this->db);
//		//$new_trip_place->place_id=$new_place_id;
//		//$new_trip_place->trip_id=$trip_id;
//		//$new_trip_place_id = $new_trip_place->insert();
//        
//	
//	}
}

?>